package cn.stylefeng.guns.modular.bms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.bms.entity.BmsIndustry;
import cn.stylefeng.guns.modular.bms.mapper.BmsIndustryMapper;
import cn.stylefeng.guns.modular.bms.model.params.BmsIndustryParam;
import cn.stylefeng.guns.modular.bms.model.result.BmsIndustryResult;
import  cn.stylefeng.guns.modular.bms.service.BmsIndustryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行业信息表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-22
 */
@Service
public class BmsIndustryServiceImpl extends ServiceImpl<BmsIndustryMapper, BmsIndustry> implements BmsIndustryService {

    @Override
    public void add(BmsIndustryParam param){
        BmsIndustry entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BmsIndustryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BmsIndustryParam param){
        BmsIndustry oldEntity = getOldEntity(param);
        BmsIndustry newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BmsIndustryResult findBySpec(BmsIndustryParam param){
        return null;
    }

    @Override
    public List<BmsIndustryResult> findListBySpec(BmsIndustryParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BmsIndustryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZTreeNode> industryTreeList() {
        return baseMapper.industryTreeList();
    }

    @Override
    public List<Map<String, Object>> selectIndustryTree(BmsIndustryParam param) {
        List<Map<String, Object>> maps = baseMapper.customMapList(param);
        if (maps == null) {
            maps = new ArrayList<>();
        }
        //修复菜单查询bug，带条件的暂时先父级置为0
        if (ToolUtil.isNotEmpty(param.getKeyword())) {
            if (maps.size() > 0) {
                //将pcode置为root
                for (Map<String, Object> menu : maps) {
                    menu.put("pid", "0");
                }
            }
        }
//        //创建根节点
//        BmsIndustryParam postInfoParam = new BmsIndustryParam();
//        postInfoParam.setName("根节点");
//        postInfoParam.setId(1L);
//        postInfoParam.setPid(0L);
//        maps.add(BeanUtil.beanToMap(postInfoParam));
        return maps;

    }

    private Serializable getKey(BmsIndustryParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BmsIndustry getOldEntity(BmsIndustryParam param) {
        return this.getById(getKey(param));
    }

    private BmsIndustry getEntity(BmsIndustryParam param) {
        BmsIndustry entity = new BmsIndustry();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
